package com.snline.crm.logistics.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import com.snline.crm.logistics.entity.ProjectDetails;
import com.snline.crm.logistics.entity.ProjectInfo;
import com.snline.crm.logistics.service.ProjectService;
import com.snline.crm.logistics.service.UserService;

public class ProjectAction extends BaseAction {

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 项目信息
	 */
	private ProjectInfo projectInfo;
    
	/**
	 * 项目信息
	 */
	private List<ProjectInfo> proList;
	
	private String saveUserId;
	
	/**
	 * 用户信息
	 */
	private List<ProjectDetails> allUserList;
	
	/**
	 * 用户信息
	 */
	private List<ProjectDetails> selectUserList;
	
	@Resource
	private ProjectService projectService;
	
	@Resource
	private UserService userService;
	
	/**
	 * 项目信息取得
	 * @return 处理结果
	 */
	public String getProjectInfoByUserId() {
		
		String userId = session.get("userId").toString();

		proList = projectService.findProjectUser(userId);
		    
		return  SUCCESS;
	}

	/**
	 * 项目信息取得
	 * @return 处理结果
	 */
	public String getMyProjectInfoByUserId() {
		
		String userId = session.get("userId").toString();

		proList = projectService.findMyProjectUser(userId);
		    
		return  SUCCESS;
	}
	
	/**
	 * 项目信息取得
	 * @return 处理结果
	 * @throws ParseException 
	 */
	
	public String getProjectInfoById() throws ParseException {
		
		allUserList = userService.findAllUserInfo();
		String pjId = servletRequest.getParameter("pjId").toString();
		projectInfo = projectService.findProjectUserById(pjId);
		selectUserList = projectService.findUserByPjId(pjId);
		ProjectDetails ps = null;
		for(ProjectDetails pa : selectUserList ){
			for(int i = 0; i < allUserList.size(); i++){
				ps = allUserList.get(i);
				if(pa.getPjUserId() == ps.getPjUserId()){
					allUserList.remove(ps);
				}
			}
		}

		SimpleDateFormat strYMD = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat strYMDHMS = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss.SSS");
		try {
			projectInfo.setPjStartTime(strYMD.format(strYMDHMS
					.parse(projectInfo.getPjStartTime())));
			projectInfo.setPjExpectEndTime(strYMD.format(strYMDHMS
					.parse(projectInfo.getPjExpectEndTime())));
			projectInfo.setPjEndTime(strYMD.format(strYMDHMS
					.parse(projectInfo.getPjEndTime())));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String del() {

        String pjId = servletRequest.getParameter("pjId").toString();
	    
        projectService.deleteProjectInfo(pjId);
	    return SUCCESS;
	}
	
	public String save() {
	    
	    if (projectInfo!= null) {
	    	String userId = session.get("userId").toString();
	    	String[] strUserId = saveUserId.split(",");
	    	projectService.deleteProjectDetails(String.valueOf(projectInfo.getPjId()));

	    	for(int i = 0;i < strUserId.length; i++){
	    		ProjectDetails projectDetails = new ProjectDetails();
	    		projectDetails.setPjId(projectInfo.getPjId());
	    		projectDetails.setPjUserId(Integer.parseInt(strUserId[i]));
	    		projectService.insertProjectUser(projectDetails);
	    	}
	    	
	    	// 负责人
	    	projectInfo.setPjManager(userId);
	    	
	        if (projectInfo.getPjId() == 0) {
	            projectService.insertProjectInfo(projectInfo);
	        } else {
                projectService.updateProjectInfo(projectInfo);
	        }
	    }
	    return SUCCESS;
	}
	
	/**
	 * @return the proList
	 */
	public List<ProjectInfo> getProList() {
		return proList;
	}

	/**
	 * @param proList the proList to set
	 */
	public void setProList(List<ProjectInfo> proList) {
		this.proList = proList;
	}

	/**
	 * @return the projectService
	 */
	public ProjectService getProjectService() {
		return projectService;
	}

	/**
	 * @param projectService the projectService to set
	 */
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	/**
	 * @param projectInfo the projectInfo to set
	 */
	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}

	/**
	 * @return the projectInfo
	 */
	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	/**
	 * @return the allUserList
	 */
	public List<ProjectDetails> getAllUserList() {
		return allUserList;
	}

	/**
	 * @param allUserList the allUserList to set
	 */
	public void setAllUserList(List<ProjectDetails> allUserList) {
		this.allUserList = allUserList;
	}

	/**
	 * @return the selectUserList
	 */
	public List<ProjectDetails> getSelectUserList() {
		return selectUserList;
	}

	/**
	 * @param selectUserList the selectUserList to set
	 */
	public void setSelectUserList(List<ProjectDetails> selectUserList) {
		this.selectUserList = selectUserList;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the saveUserId
	 */
	public String getSaveUserId() {
		return saveUserId;
	}

	/**
	 * @param saveUserId the saveUserId to set
	 */
	public void setSaveUserId(String saveUserId) {
		this.saveUserId = saveUserId;
	}
	
	
}
